﻿$.fn.togglepanels = function () {
    var container = $(this);
    var cookieValue = $.cookie("panelsState");
    var parsedValue = [];

    if (cookieValue) {
        cookieValue = cookieValue.split(",");
        $(cookieValue).each(function (index) {
            if (parseInt(this) != -1) {
                parsedValue.push(parseInt(this));
            }
        });
        cookieValue = parsedValue;
    }
    else {
        //default values
        cookieValue = [0, 1, 3, 4];
    }
    //console.log('loaded:', cookieValue);
    var updateCookies = function () {
        var panels = [];
        panels.push(-1);
        container.find('h3').each(function (index) {
            if ($(this).hasClass('ui-state-active')) {
                panels.push(index);
            }
        });
        $.cookie('panelsState', panels, { path: '/' });
        //console.log('saved:', panels);
    }

    return this.each(function (index) {
        $(this).addClass("ui-accordion ui-accordion-icons ui-widget ui-helper-reset")
          .find("h3").each(function (index) {
              if ($.inArray(index, cookieValue) >= 0) {
                  $(this).addClass("ui-accordion-header ui-helper-reset ui-corner-top ui-accordion-header-active ui-state-active")
                   .hover(function () { $(this).toggleClass("ui-state-hover"); })
                   //.prepend('<span class="ui-icon ui-icon-triangle-1-e ui-icon-triangle-1-e ui-icon-triangle-1-s"></span>')
                   .prepend('<span class="ui-icon ui-icon-triangle-1-s"></span>')
                    .click(function () {
                        $(this)
                            .toggleClass("ui-accordion-header-active ui-state-active ui-state-default ui-corner-bottom")
                            .find("> .ui-icon").toggleClass("ui-icon-triangle-1-e ui-icon-triangle-1-s").end()
                            .next().slideToggle();
                        updateCookies();
                        return false;
                    })
                   .next()
                   .addClass("ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom")
                   .show();

              }
              else {
                  $(this).addClass("ui-accordion-header ui-helper-reset ui-state-default ui-corner-top ui-corner-bottom")
                  .hover(function () { $(this).toggleClass("ui-state-hover"); })
                  .prepend('<span class="ui-icon ui-icon-triangle-1-e"></span>')
                  .click(function () {
                      $(this)
                        .toggleClass("ui-accordion-header-active ui-state-active ui-state-default ui-corner-bottom")
                        .find("> .ui-icon").toggleClass("ui-icon-triangle-1-e ui-icon-triangle-1-s").end()
                        .next().slideToggle();
                      updateCookies();
                      return false;
                  })
                  .next()
                    .addClass("ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom")
                    .hide();
              }
          });
    });
};